package com.abhinav;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class MapDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Dog d1 = new Dog("red", 30);
		Dog d2 = new Dog("black", 20);
		Dog d3 = new Dog("white", 10);
		Dog d4 = new Dog("white", 40);

		Map<Dog, Integer> map = new LinkedHashMap<Dog, Integer>();
		map.put(d1, 10);
		map.put(d2, 15);
		map.put(d3, 5);
		map.put(d4, 20);
		System.out.println("***********LinkedHashMap*************");
		for (Map.Entry<Dog, Integer> entry : map.entrySet()) {
			System.out.println(entry.getKey() + " - " + entry.getValue());
		}
		
		map = new HashMap<Dog, Integer>();
		map.put(d1, 10);
		map.put(d2, 15);
		map.put(d3, 5);
		map.put(d4, 20);
		System.out.println("************HashMap************");
		for (Map.Entry<Dog, Integer> entry : map.entrySet()) {
			System.out.println(entry.getKey() + " - " + entry.getValue());
		}
		
		map = new TreeMap<Dog, Integer>();
		map.put(d1, 10);
		map.put(d2, 15);
		map.put(d3, 5);
		map.put(d4, 20);
		System.out.println("***********TreeMap*************");
		for (Map.Entry<Dog, Integer> entry : map.entrySet()) {
			System.out.println(entry.getKey() + " - " + entry.getValue());
		}
		
	}
}

class Dog implements Comparable {
	String color;
	int size;

	Dog(String c, int s) {
		color = c;
		size = s;
	}

	public String toString() {
		return color + " dog";
	}

	@Override
	public int compareTo(Object o) {
		return ((Dog) o).size - this.size;
	}

	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj == null || obj.getClass() != this.getClass()) {
			return false;
		}

		Dog guest = (Dog) obj;
		
		return size == guest.size && (color == guest.color || (color != null && color.equals(guest.color)));

	}

	public int hashCode() {
		int hash = 31;
		hash = 17 * hash + (this.color != null ? this.color.hashCode() : 0);
		hash = 89 * hash + (int) (this.size ^ (this.size >>> 32));

		return hash;
	}

}
